package com.abe.bathsystem.widget.video;

import android.content.Context;
import android.media.AudioManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.abe.libcore.utils.normal.CommonUtils;

import cn.jzvd.JZMediaManager;
import cn.jzvd.JZUserAction;
import cn.jzvd.JZUtils;
import cn.jzvd.JZVideoPlayerStandard;

public class AdVideoAutoPlayer extends JZVideoPlayerStandard {

    public interface OnVideoPlayListener {
        void onVideoPrepare();

        void onVideoStart();

        void onVideoError();

        void onVideoPlay();

        void onVideoPause();

        void onStateAutoComplete();

        void onAutoCompletion();
    }

    public AdVideoAutoPlayer(Context context) {
        super(context);
    }

    public AdVideoAutoPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private OnVideoPlayListener playListener;

    public void setPlayListener(OnVideoPlayListener playListener) {
        this.playListener = playListener;
    }

    @Override
    public void onStatePreparing() {
        super.onStatePreparing();
        CommonUtils.w("AdVideoAutoPlayer-onStatePreparing");
        if (playListener != null) {
            playListener.onVideoPrepare();
        }
    }

    @Override
    public void onStatePrepared() {
        super.onStatePrepared();
        CommonUtils.w("AdVideoAutoPlayer-onStatePrepared");
        if (playListener != null) {
            playListener.onVideoStart();
        }
    }

    @Override
    public void onStatePlaying() {
        super.onStatePlaying();
        CommonUtils.w("AdVideoAutoPlayer-onStatePlaying");
        if (playListener != null) {
            playListener.onVideoPlay();
        }
    }

    @Override
    public void onStateError() {
        super.onStateError();
        CommonUtils.w("AdVideoAutoPlayer-onStateError");
        if (playListener != null) {
            playListener.onVideoError();
        }
    }

    @Override
    public void onStatePause() {
        super.onStatePause();
        if (playListener != null) {
            playListener.onVideoPause();
        }
    }

    @Override
    public void onStateAutoComplete() {
        CommonUtils.w("AdVideoAutoPlayer-onStateAutoComplete");
        currentState = CURRENT_STATE_AUTO_COMPLETE;
        cancelProgressTimer();
        progressBar.setProgress(100);
        currentTimeTextView.setText(totalTimeTextView.getText());
        changeUiToComplete();
        cancelDismissControlViewTimer();
        bottomProgressBar.setProgress(100);
        if (playListener != null) {
            playListener.onStateAutoComplete();
        }
    }

    @Override
    public void onAutoCompletion() {
        Runtime.getRuntime().gc();
        onEvent(JZUserAction.ON_AUTO_COMPLETE);
        dismissVolumeDialog();
        dismissProgressDialog();
        dismissBrightnessDialog();
        onStateAutoComplete();
        //JZMediaManager.instance().releaseMediaPlayer();
        //重置进度为0
        JZUtils.saveProgress(getContext(), JZUtils.getCurrentFromDataSource(dataSourceObjects, currentUrlMapIndex), 0);
        cancelDismissControlViewTimer();
        if (playListener != null) {
            playListener.onAutoCompletion();
        }
        CommonUtils.w("AdVideoAutoPlayer-onAutoCompletion");
    }

    @Override
    public void onCompletion() {
        Log.i(TAG, "onCompletion " + " [" + this.hashCode() + "] ");
        if (currentState == CURRENT_STATE_PLAYING || currentState == CURRENT_STATE_PAUSE) {
            long position = getCurrentPositionWhenPlaying();
            JZUtils.saveProgress(getContext(), JZUtils.getCurrentFromDataSource(dataSourceObjects, currentUrlMapIndex), position);
        }
        cancelProgressTimer();
        dismissBrightnessDialog();
        dismissProgressDialog();
        dismissVolumeDialog();
        onStateNormal();
        textureViewContainer.removeView(JZMediaManager.textureView);
        JZMediaManager.instance().currentVideoWidth = 0;
        JZMediaManager.instance().currentVideoHeight = 0;

        AudioManager mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
        if (mAudioManager != null) mAudioManager.abandonAudioFocus(onAudioFocusChangeListener);
        //JZUtils.scanForActivity(getContext()).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //clearFullscreenLayout();
        JZUtils.setRequestedOrientation(getContext(), NORMAL_ORIENTATION);

        if (JZMediaManager.surface != null) JZMediaManager.surface.release();
        if (JZMediaManager.savedSurfaceTexture != null)
            JZMediaManager.savedSurfaceTexture.release();
        JZMediaManager.textureView = null;
        JZMediaManager.savedSurfaceTexture = null;
        cancelDismissControlViewTimer();
        if (clarityPopWindow != null) {
            clarityPopWindow.dismiss();
        }
    }

    @Override
    public void changeUiToComplete() {
        switch (currentScreen) {
            case SCREEN_WINDOW_NORMAL:
            case SCREEN_WINDOW_LIST:
                setAllControlsVisiblity(View.INVISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
                //updateStartImage();
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisiblity(View.INVISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
                //updateStartImage();
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }
    }
}
